#include "HiRtpCommonUtil.h"
#include "ParserPacket.hpp"
#include "RTPDecoderHandle.hpp"
#include "RTPDecoderWrapper.hpp"
#include "RtpPacket.hpp"
#include "UdpSocketWrapper.hpp"
#include <arpa/inet.h>
#include <gtest/gtest.h>
#include <sstream>

static const char *myIp = "127.0.0.1";

using namespace Hi;
using namespace std;
static Hi::RTPDecoderWrapper decoder;

static uint32_t s_index = 0;
class MyRTPDecoderHandle : public Hi::RTPDecoderHandle {
  public:
    void OnYUV(const uint8_t *data, size_t len, uint32_t width, uint32_t height,
               void *) {}
    void OnRGB(const uint8_t *data, size_t len, uint32_t width, uint32_t height,
               void *) {
        stringstream oss;
        oss << "./bmp/" << s_index % 100 << ".bmp";
        hi_saveBmp(oss.str().c_str(), data, width, height);
        s_index++;
    }
};

static void UdpReceiveMsg(const uint8_t *data, size_t len,
                          const struct sockaddr *addr,
                          UdpSocketWrapper *wrapper) {
    RtpPacket *packet = hi_RtpPacket_parse(data, len);
    if (packet == nullptr) {
        return;
    }
    decoder.Decode(packet);
    hi_RtpPacket_release(&packet);
}

int main(int argc, char *argv[]) {
    system("mkdir bmp");
    system("rm -fr bmp/*");
    MyRTPDecoderHandle handle;
    decoder.Start(handle, 1024, 576, nullptr);
    UdpSocketWrapper *udp = new UdpSocketWrapper();
    udp->Listen(myIp, 60001, UdpReceiveMsg);
    while (true) {
        sleep(10);
    }
}